package com.example.hospitalsystem.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hospitalsystem.exception.HospitalSystemServiceException;
import com.example.hospitalsystem.mapper.AdminMapper;
import com.example.hospitalsystem.mapper.NoticeMapper;
import com.example.hospitalsystem.mapper.UserMapper;
import com.example.hospitalsystem.pojo.dto.user.*;
import com.example.hospitalsystem.pojo.entity.Admin;
import com.example.hospitalsystem.pojo.entity.Notice;
import com.example.hospitalsystem.pojo.entity.Registration;
import com.example.hospitalsystem.pojo.entity.User;
import com.example.hospitalsystem.pojo.vo.UserCommenVo;
import com.example.hospitalsystem.result.Result;
import com.example.hospitalsystem.result.ResultCodeEnum;
import com.example.hospitalsystem.service.UserService;
import com.example.hospitalsystem.utils.JwtHelper;
import com.example.hospitalsystem.utils.PaginationResult;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Autowired JwtHelper jwtHelper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private AdminMapper adminMapper;


    @Override
    public Result<Object> getUserInfo() {
        String token=request.getHeader("Authorization");
        Integer userId= jwtHelper.getUserId(token);
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUserId,userId);

        User user=baseMapper.selectOne(lambdaQueryWrapper);

        UserCommenVo userCommenVo=new UserCommenVo();
        userCommenVo.setUsername(user.getUsername());
        userCommenVo.setPassword(user.getPassword());
        userCommenVo.setEmail(user.getEmail());
        return Result.build(userCommenVo,200,"获取用户个人信息成功");
    }

    @Override
    public Result<Object> getAllNomerlUserInfo(UserPageQueryDto userPageQueryDto) {

        int pageNum=userPageQueryDto.getPageNum();
        int pageSize=userPageQueryDto.getPageSize();
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("role",0);
        //创建page对象
        Page<User> userPage=new Page<>(pageNum,pageSize);
        IPage<User>userIPage=baseMapper.selectPage(userPage,queryWrapper);
        return Result.build(userIPage,"获取所有普通用户信息成功");
    }

    @Override
    public Result<Object> getAllAdmin(AdminPageQueryDto adminPageQueryDto) {
        int pageNum=adminPageQueryDto.getPageNum();
        int pageSize=adminPageQueryDto.getPageSize();
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("role",2);
        //创建page对象
        Page<User> adminPage=new Page<>(pageNum,pageSize);
        IPage<User>adminIPage=baseMapper.selectPage(adminPage,queryWrapper);

        return Result.build(adminIPage,"获取所有管理员信息成功");
    }

    @Override
    public Result<Object> deleteUserInfo(Integer id) {
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUserId,id);

        User user=this.getOne(lambdaQueryWrapper);
        if(user==null){
            return Result.build(null, ResultCodeEnum.USER_ERROR);
        }else{
            this.removeById(id);
            return Result.build(null,"删除用户信息成功");
        }
    }

    @Override
    public Result<Object> deleteAdminInfo(Integer id) {
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUserId,id);

        User user=this.getOne(lambdaQueryWrapper);
        if(user==null){
            return Result.build(null, ResultCodeEnum.USER_ERROR);
        }else{
            this.removeById(id);
            return Result.build(null,"删除管理员信息成功");
        }
    }

    @Override
    public Result<Object> editUserInfo(EditDto editDto) {
        try{
            LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(User::getUserId,editDto.getUserId());

            User user=this.getOne(lambdaQueryWrapper);
            BeanUtils.copyProperties(editDto,user);
            this.updateById(user);
            return Result.build(null,"修改成功");
        }catch (Exception e){
            return Result.build(null,"修改数据失败");
        }
    }

    @Override
    public Result<Object> editAdminInfo(EditDto editDto) {
        try{
            LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(User::getUserId,editDto.getUserId());

            User user=this.getOne(lambdaQueryWrapper);
            BeanUtils.copyProperties(editDto,user);
            this.updateById(user);
            return Result.build(null,"修改成功");
        }catch (Exception e){
            return Result.build(null,"修改数据失败");
        }
    }

    @Override
    public Result<Object> getRoleByUserName(LookRoleByUnDto lookRoleByUnDto) {
        LambdaQueryWrapper<User> userLambdaQueryWrapper=new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getUsername,lookRoleByUnDto.getUsername1());

        int role=userMapper.selectOne(userLambdaQueryWrapper).getRole();
        return Result.build(role,200,"获取角色信息成功");
    }

    @Override
    public Result<Object> getRoleByEmail(LookRoleByEmail lookRoleByEmail) {
        LambdaQueryWrapper<User> userLambdaQueryWrapper=new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getEmail,lookRoleByEmail.getEmail());

        int role=userMapper.selectOne(userLambdaQueryWrapper).getRole();
        return Result.build(role,200,"获取角色信息成功");
    }

    @Override
    public Result<Object> pushNotice(NoticeDto noticeDto) {
        String token=request.getHeader("Authorization");
        Integer userId= jwtHelper.getUserId(token);

        LambdaQueryWrapper<Admin>lambdaQueryWrapper1=new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(Admin::getUserId,userId);
        String adminName=adminMapper.selectOne(lambdaQueryWrapper1).getAdminName();

        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUserId,userId);
        String userName=userMapper.selectOne(lambdaQueryWrapper).getUsername();


        LocalDateTime localDateTime = LocalDateTime.now();
        Timestamp timestamp = Timestamp.valueOf(localDateTime);
        Notice notice=new Notice();
        notice.setUserName(userName);
        notice.setPublishTime(timestamp);
        notice.setContent(noticeDto.getContent());

        if(adminName!=null&&adminName!=""){
            notice.setName(adminName);
        }
        noticeMapper.insert(notice);

        return Result.build(notice,200,"发布通告信息成功");
    }

    @Override
    public Result<Object> getImage() {
        String token=request.getHeader("Authorization");
        Integer userId= jwtHelper.getUserId(token);
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUserId,userId);

        User user=baseMapper.selectOne(lambdaQueryWrapper);

        return Result.build(user.getImage(),200,"返回照片信息成功");
    }

    @Override
    public Result<Object> changeUserInfo(User user) {
        String token=request.getHeader("Authorization");
        Integer userId= jwtHelper.getUserId(token);
        LambdaUpdateWrapper<User> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(User::getUserId,userId);

        User user1=baseMapper.selectOne(lambdaUpdateWrapper);
        BeanUtils.copyProperties(user,user1);
        this.update(user,lambdaUpdateWrapper);
        return Result.build("更改成功");
    }

    @Override
    public List<User> searchUser(String key) {
        List<User> list = userMapper.search(key);
        List<User> userList = list.stream().filter(user -> user.getRole().equals(0)).collect(Collectors.toList());
        return userList;
    }

}
